package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ChargeStationGunMapper;
import com.zhentao.mapper.ChargeStationInfoMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pojo.core.ChargeRecord;
import com.zhentao.model.pojo.core.ChargeStationGun;
import com.zhentao.model.pojo.core.ChargeStationInfo;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.service.ChargeRecordService;
import com.zhentao.mapper.ChargeRecordMapper;
import com.zhentao.user.ChargeUserThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 31919
* @description 针对表【charge_record】的数据库操作Service实现
* @createDate 2024-12-22 19:57:12
*/
@Service
public class ChargeRecordServiceImpl extends ServiceImpl<ChargeRecordMapper, ChargeRecord>
    implements ChargeRecordService{
    @Autowired
    private ChargeStationGunMapper chargeStationGunMapper;
    @Autowired
    private ChargeStationInfoMapper chargeStationInfoMapper;
    @Autowired
    private ChargeRecordMapper chargeRecordMapper;
    /**
     * @description   我的充电订单列表展示
     * @author 大毅
     * @param payStatus
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult getChargeRecordList(Integer payStatus) {
        ChargeUser user = ChargeUserThreadUtils.getChargeUserThreadLocal();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
//        List<ChargeRecord> chargeRecordList = chargeRecordMapper.getChargeRecordList(user.getId(), payStatus);
//        return ResponseResult.okResult(chargeRecordList);
        LambdaQueryWrapper<ChargeRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChargeRecord::getPayStatus, payStatus);
        wrapper.eq(ChargeRecord::getUserId, user.getId());
        List<ChargeRecord> recordList = list(wrapper);
        for (ChargeRecord record : recordList) {
            Integer stationGunId = record.getStationGunId();
            ChargeStationGun chargeStationGun = chargeStationGunMapper.selectById(stationGunId);
            record.setChargeStationGun(chargeStationGun);
        }
        for (ChargeRecord record : recordList) {
            Integer stationId = record.getChargeStationGun().getStationId();
            ChargeStationInfo stationInfo = chargeStationInfoMapper.selectById(stationId);
            record.getChargeStationGun().setChargeStationInfo(stationInfo);
        }
        return ResponseResult.okResult(recordList);
    }
}




